<template>
    <Modal :mask-closable="false" v-model="toggle" :title="title" class="modal-content-layout">
        <div v-if="toggle">
            <Form ref="form" :model="form" :rules="rules" label-position="top">
                <FormItem :label="$t(desc)" prop="value">
                    <Input maxlength="200" v-model="form.value" :type="type || 'textarea'" :rows="5" />
                </FormItem>
            </Form>
        </div>

        <div slot="footer">
            <Button type="default" @click="cancel">{{ $t('取消') }}</Button>
            <Button type="primary" @click="submit">{{ $t('确认') }}</Button>
        </div>
    </Modal>
</template>


<script>

export default {
    name: "inputModal",
    props: ['id'],
    data() {
        return {
            toggle: false,
            form: {
                value: "",
            },
            type: "",
            title: "",
            desc: "",
            rules: {
                value: [{ required: true, type: "string", message: this.$t('请输入'), trigger: 'blur' }],
            }
        }
    },
    methods: {
        showModal({ submit, title, desc, required ,type}) {
            this.form.value = ''
            this.$set(this.rules.value[0], 'required', required==false ? false : true)
            this.desc = desc || '原因'
            this.title = title
            this.type = type
            this.toggle = true
            this.$nextTick(()=>{
                  this.$refs.form.resetFields()
            })
            this.onSubmit = () => {
                if (typeof submit === 'function'){
                    submit(this.form)
                    this.cancel()  
                }
            }
        },
        cancel() {
            this.$refs.form.resetFields()
            this.toggle = false
            this.form.value = '';
        },
        onSubmit() {

        },
        async submit() {
            const valid = await this.$refs.form.validate()
            if (valid) {
                this.onSubmit()
            }
        }
    }
}
</script>
